package com.caishi.lkx.course.api;

import cn.hutool.core.util.StrUtil;
import com.caishi.lkx.common.AdminBaseApi;
import com.caishi.lkx.common.ienum.ClientType;
import com.caishi.lkx.common.role.Roles;
import com.caishi.lkx.course.model.VideoChapterModel;
import com.caishi.lkx.course.service.IVideoChapterService;
import com.caishi.lkx.exam.records.RecordsGetDataService;
import com.caishi.lkx.exam.service.IColumnService;
import com.caishi.lkx.exam.service.IExColumnConfigReService;
import com.zzw.common.exception.BizException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;

/**
 * @author lkx
 * &#064;date  2024/5/24
 * &#064;
 */
@Tag(name = "视频章节")
@Slf4j
@RestController
@RequestMapping("/kc/course/video/chapter")
public class VideoChapterApi extends AdminBaseApi<IVideoChapterService, VideoChapterModel, String> {

    @Resource
    private IVideoChapterService service;

    @Resource
    private IExColumnConfigReService exColumnConfigReService;

    @Resource
    private RecordsGetDataService recordsGetDataService;

    @Resource
    private IColumnService columnService;

    @Override
    public IVideoChapterService loadService() {
        return service;
    }

    @GetMapping("/detail/{id}")
    @Roles
    @Override
    public VideoChapterModel detail(@PathVariable String id) {
        return super.detail(id);
    }

    @PutMapping("/change-video-count")
    public Boolean changeVideoCount(@RequestParam String id){

        loadService().changeVideoCount(id);

        return true;
    }
    @Operation(summary = "通过栏目获取章节")
    @GetMapping("/list-by-column")
    public List<VideoChapterModel> listByColumn(@RequestParam String industryCode,
                                                @RequestParam String examId,
                                                @RequestParam Long categoryId,@RequestParam String columnId){

        List<VideoChapterModel> chapterList = exColumnConfigReService.getVideoChaptersByColumn(columnId);
        String userId = userContext.currentUserId();
        if(StrUtil.isNotBlank(userId)){
            chapterList.forEach(item->{
                    item.setWatchNum(recordsGetDataService.getCountVideoRecordsData(userId, industryCode, examId, categoryId,columnId, item.getId()).getVn());
                    try {
                        if (!item.getFree())
                            item.setFree(columnService.currentHaveAuth(null, null, userId, categoryId, columnId, ClientType.zyksApp));
                    } catch (BizException e) {
                        throw new RuntimeException(e);
                    }
            });

        }
        return chapterList;
    }

    @GetMapping("/child-chapter")
    @Roles
    @Operation(summary = "获取子章节")
    @Parameters({@Parameter(name = "pid", description = "父级章节id", required = true),})
    public List<VideoChapterModel> getChapterChildList(@RequestParam String industryCode,
                                                       @RequestParam String examId,
                                                       @RequestParam Long categoryId,
                                                       @RequestParam String columnId,
                                                       @RequestParam String pid) {
        List<VideoChapterModel> list = service.selectNextChildren(pid);
        return list.stream().sorted(Comparator.comparing(VideoChapterModel::getSort)).peek(v -> {
            String userId = userContext.currentUserId();
            if(StrUtil.isNotBlank(userId))
                v.setWatchNum(recordsGetDataService.getCountVideoRecordsData(userId, industryCode, examId, categoryId,columnId, v.getId()).getVn());

        }).toList();
    }

}
